<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreFixfxs;
use App\Http\Requests\UpdateFixfxs;
use App\Models\Fixfxs;
use App\Models\Fixfxscate;
use App\Models\FixfxsTag;
use App\Models\Img;
use App\Models\Onorder;
use App\Models\Store;
use App\Models\Tag;
use function Couchbase\defaultDecoder;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Pagination\LengthAwarePaginator;

class FixfxsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


    public function __construct()
    {
        view()->share([
            '_ygs' => 'am-in',
            '_fixfxs' => 'am-active',
        ]);

    }

//教练管理首页
    public function index(Request $request)
    {

        $where = function ($query) use ($request) {
            //按教练名字名称
            if ($request->has('name') and $request->name != '') {
                $search = "%" . $request->name . "%";
                $query->where('name', 'like', $search);
            }
            //按门店名称
            if ($request->has('store_id') and $request->store_id != '-1') {

                $query->where('store_id', $request->store_id);
            }
        };
        //全部门店
        $cates = Store::all();
        //教练关联门店，标签
        $fixfxs = Fixfxs::where($where)->with('store')->with('tags')->orderBy('store_id')->paginate(20);

        return view('admin.fixfxs.index', compact('fixfxs', 'cates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //新增页面
    public function create()
    {
        //全部标签
        $tags = Tag::all();
        //全部门店
        $cates = Store::all();

        return view('admin.fixfxs.create', compact('tags', 'cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    //执行新增
    public function store(StoreFixfxs $request)
    {
        // 除了imgs,file,tag之外的全部字段
        $data = $request->except('imgs', 'file', 'tag');
        //新增教练基本信息
        $fxs = Fixfxs::create($data);

        $imgs = $request->imgs;
        $tags = $request->tag;
        //新增教练标签
        foreach ($tags as $tag) {
            $info['fixfxs_id'] = $fxs->id;
            $info['tag_id'] = $tag;
            FixfxsTag::create($info);
        }
        //新增教练作品图
        if ($imgs) {
            foreach ($imgs as $img) {
                $item['fixfxs_id'] = $fxs->id;
                $item['img'] = $img;
                Img::create($item);
            }
        }

        return redirect('/admin/fixfxs')->with('success', '新增教练成功');


    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //编辑教练页面
    public function edit($id)
    {
        //全部标签
        $tags = Tag::all();
        //全部门店
        $cates = Store::all();
        //当前教练
        $fixfxs = Fixfxs::with('store')->with('imgs')->find($id);
        //当前教练包含的tag_id，集合转化成数据
        $tag_id = FixfxsTag::where('fixfxs_id', $id)->pluck('tag_id')->toArray();

        return view('admin.fixfxs.edit', compact('fixfxs', 'cates', 'tags', 'tag_id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //执行更新
    public function update(UpdateFixfxs $request, $id)
    {
        //当前教练
        $fixfxs = Fixfxs::find($id);
        $info['name'] = $request->name;
        $about = Fixfxs::where($info)->first();
        $item = Fixfxs::where('number', $request->number)->first();
        $data = $request->except('imgs', 'file', 'tag');
        //保证教练姓名唯一性
        if ($request->name != $fixfxs['name'] && isset($about)) {
            return back()->with('success', '.所修改名字已经有人使用，请取个别名');
        }
        if ($request->number != $fixfxs['number'] && isset($item)) {
            return back()->with('success', '所修改编号已经有人使用，请另取编号');
        }
        $fixfxs->update($data);
        //同步更新标签
        $fixfxs->tags()->sync($request->tag);

        if ($request->has('imgs')) {
            $imgs = $request->imgs;

            foreach ($imgs as $img) {
                $some['fixfxs_id'] = $id;
                $some['img'] = $img;
                //新增新添加的图片
                Img::create($some);
            }

        }
        return redirect('/admin/fixfxs')->with('success', '修改成功');

    }

    /*
     * 执行删除教练
     * */

    public function del($id){
        $del=Fixfxs::destroy($id);
        if($del){
            return redirect('/admin/fixfxs')->with('success','删除成功');
        }
    }


    /***
     * 教练编辑时ajax删除相册照片
     */
    public function destroy_gallery(Request $request)
    {

        Img::destroy($request->gallery_id);

        return $data = 1;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {


//        先要判断当前发型师有没有预约订单
        $info = Onorder::where('fixfxs_id', $id)->where('statu', 0)->pluck('bespeak_at')->toarray();

        foreach ($info as $k => $i) {
            if (time() - strtotime($i) < 7200) {
                return back()->with('success', '所删除教练有私教课程不能删除');
            }
        }
        Fixfxs::destroy($id);
        //删除标签教练中间表关系数据
        $tag_id = FixfxsTag::where('fixfxs_id', $id)->pluck('tag_id')->toarray();
        FixfxsTag::whereIn('tag_id', $tag_id)->delete();
        //删除教练作品图
        Img::where('fixfxs_id', $id)->delete();
        return back()->with('success', '删除成功');

    }

    //改变状态
    public function change_attr(Request $request)
    {

        $fxs = Fixfxs::find($request->id);

        if ($fxs->is_use == 0) {
            $data['is_use'] = 1;
            $fxs->update($data);
        } else {
            $data['is_use'] = 0;
            $fxs->update($data);
        }
    }


}
